package org.study;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.*;

public class SocketProxyDemo {


    public static void main(String[] args) throws MalformedURLException {
        // 代理服务器地址和端口
        String proxyHost = "127.0.0.1";
        int proxyPort = 7890;

        // 创建一个代理服务器对象
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));


        URL url = new URL("http://www.baidu.com/");
        try {
            URLConnection urlConnection = url.openConnection(proxy);
            urlConnection.setConnectTimeout(2000);
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
